
//
// 101206 - AH - Created.
// 101225a- AH - Missing java doc added.
// 101225b- AH - Uses SoekratSwingUtil.showUserInputErrDlg to show end user err
//               message.
// 101225c- AH - Now constructors accepts SoekratEngine and HistoricalEvent
//               objects are generated.
//

package org.soekrat.gui.swing.forest;

import static org.soekrat.gui.swing.SoekratSwingUtil.showUserInputErrDlg;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;

import org.soekrat.engine.Soekrat2Engine;
import org.soekrat.engine.forest.Tree;

/**
 * <p>
 *   Dialog for creating a 
 *   {@link Tree}.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
@SuppressWarnings("serial")
public final class TreeCreatorDialog extends JDialog 
{
  private CreateEditTreePanel m_ctp;
  
  private Tree m_parent=null;
  
  private Tree m_created=null;
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param tc     Object responsible for creating 
   *               {@link Tree}s.
   * @param engine {@link Soekrat2Engine} interfaced.            
   */
  public TreeCreatorDialog(TreeCreator tc, Soekrat2Engine engine)
  { 
    buildGui(tc, engine); 
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p      Dialog's parent.
   * @param tc     Object responsible for creating 
   *               {@link Tree}s. 
   * @param engine {@link Soekrat2Engine} interfaced.           
   */
  public TreeCreatorDialog(Frame p, TreeCreator tc, Soekrat2Engine engine)
  { 
    super(p); 
    buildGui(tc, engine); 
  }
  
  /**
   * <p>
   *   Constructor.
   * </p>
   * @param p      Dialog's parent.
   * @param tc     Object responsible for creating 
   *               {@link Tree}s. 
   * @param engine {@link Soekrat2Engine} interfaced.
   */
  public TreeCreatorDialog(Dialog p, TreeCreator tc, Soekrat2Engine engine)
  { 
    super(p); 
    buildGui(tc, engine); 
  }
  
  // Invoked from the constructors to create GUI.
  private void buildGui(TreeCreator tc, final Soekrat2Engine engine)
  {
    if (tc==null)
    {
      throw new NullPointerException("tc");
    }
    if (engine==null)
    {
      throw new NullPointerException("engine");
    }
    
    setTitle("Create Tree");
    setModal(true);
    
    setLayout(new BorderLayout());
  
    m_ctp=new CreateEditTreePanel(tc);
    add(m_ctp, BorderLayout.CENTER);
    
    JPanel buttons=new JPanel(new FlowLayout(FlowLayout.CENTER));
    buttons.add
    (
      new JButton
      (
        new AbstractAction("OK") 
        {
          public void actionPerformed(ActionEvent e) 
          {
            m_created=null;
            
            String err=m_ctp.create(m_parent);
            if (err==null)
            {
              m_created=m_ctp.getTree();
              dispose();
            }
            else
            {
              showUserInputErrDlg(TreeCreatorDialog.this, err);  
            }
          }
        }
      )
    );
    buttons.add
    (
      new JButton
      (
        new AbstractAction("Cancel") 
        {
          public void actionPerformed(ActionEvent e) 
          {
            m_created=null;
            dispose();
          }
        }
      )
    );
    add(buttons, BorderLayout.SOUTH);
    
    pack();
  }
  
  public Tree showDialog(Tree parent)
  {
    m_parent=parent;
    
    setVisible(true);
    
    return m_created;
  }
  
}
